package com.cuotibao.zhuoxiao.basics.utils.permission;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hdx on 2019/5/6.
 */
public class PermissionManager {
    private static PermissionManager mPermissionManager;
    private Map<Integer, Runnable> mAllowablePermissionRunnables = new HashMap<>();
    private Map<Integer, Runnable> mDisallowablePermissionRunnables = new HashMap<>();

    public static PermissionManager getInstances() {
        if (mPermissionManager == null) {
            mPermissionManager = new PermissionManager();
        }
        return mPermissionManager;
    }

    public void requestPermission(int requestCode, String[] permission, Runnable allowableRunnable, Runnable disallowableRunnable, Activity activity) {
        if (allowableRunnable != null) {
            mAllowablePermissionRunnables.put(requestCode, allowableRunnable);
        }
        if (disallowableRunnable != null) {
            mDisallowablePermissionRunnables.put(requestCode, disallowableRunnable);
        }
        if (Build.VERSION.SDK_INT >= 23) {
            if (verifyStoragePermissions(activity, permission, requestCode) == PermissionState.NO_REQUEST_PERMIS) {
                allow(allowableRunnable);
            }
        } else {
            allow(allowableRunnable);
        }
    }

    private void allow(Runnable allowableRunnable) {
        if (allowableRunnable != null) {
            allowableRunnable.run();
        }
    }

    public void onDestroy() {
//        mAllowablePermissionRunnables.clear();
//        mDisallowablePermissionRunnables.clear();
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull int[] grantResults) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Runnable allowRun = mAllowablePermissionRunnables.get(requestCode);
            if (allowRun != null) {
                allowRun.run();
            }
        } else {
            Runnable disallowRun = mDisallowablePermissionRunnables.get(requestCode);
            if (disallowRun != null) {
                disallowRun.run();
            }
        }
    }

    private PermissionState verifyStoragePermissions(Context context, String[] verifyPermissions, int resultCode) {
        List<String> list = new ArrayList<>();
        for (String verifyPermission : verifyPermissions) {
            if (!verifyPermission.equals("")) {
                if (ActivityCompat.checkSelfPermission(context, verifyPermission) != PackageManager.PERMISSION_GRANTED) {
                    list.add(verifyPermission);
                }
            }
        }
        String[] permissions = ListTransformString(list);
        if (permissions.length != 0) {
            ActivityCompat.requestPermissions((Activity) context, permissions,
                    resultCode);
            return PermissionState.PERMIS;
        } else {
            return PermissionState.NO_REQUEST_PERMIS;
        }
    }

    private String[] ListTransformString(List<String> list) {
        return list.toArray(new String[list.size()]);
    }

    enum PermissionState {
        NO_REQUEST_PERMIS,
        PERMIS
    }
}
